package com.jinyu.jyly.ui.home.articledetail;

import android.content.Intent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.ArticleDetailInfo;
import com.jinyu.jyly.utils.AES;


import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文章详情
 */
public class ArticleDetailActivity extends BaseActivity<ArticleDetailPresenter> implements ArticleDetailContract.View {

    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout mRlTitleView;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.webview)
    WebView mWebview;
    private ArticleDetailPresenter mPresenter;

    @Override
    public int getLayout() {
        return R.layout.activity_article_detail;
    }

    @Override
    protected ArticleDetailPresenter getPresenter() {
        if(mPresenter == null){
            mPresenter = new ArticleDetailPresenter(this, this);
        }
        return mPresenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        initIntent();
        initWebView();

    }

    private void initIntent() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        try {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("id",id);
            String param = AES.encryptToHexString(jsonObject1.toString());
            mPresenter.getArticleDetail(param);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    private void initWebView() {
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);//支持js
        webSettings.setDomStorageEnabled(true);
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置缓存
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    @Override
    public void onArtileDetail(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()){
            case 200:
                String data = infoBean.getData();
                try {
                    String result = AES.decrypt(data);
                    Gson gson = new Gson();
                    ArticleDetailInfo mallDetailInfo = gson.fromJson(result, ArticleDetailInfo.class);
                    if(mallDetailInfo != null){
                        mTvTime.setText(mallDetailInfo.getCreate_time());
                        mTvTitle.setText(mallDetailInfo.getName());

                        String content = mallDetailInfo.getContent();
                        String linkCss = "<link rel=\"stylesheet\" href=\"file:///android_asset/html.css\" type=\"text/css\"/>";
                        String body = "<html><header>" + linkCss + "</header>" + content
                                + "</body></html>";
                        mWebview.loadDataWithBaseURL(null,body, "text/html", "utf-8", null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }
}
